package suanfa.datastructure;

import java.util.Arrays;

public class Myarray {

    private int size;
    public int[] array;
    public int ca=10;

    public Myarray(){//无参构造
        this.array=new int[this.ca];
        size=0;
    }

    public Myarray(int a){//有参构造
        this.array=new int[a];
        size=0;
    }

    public void add(int d){//添加元素
        array[size++]=d;
    }


    public void resize(int a){//增加数组长度
        int[] newarray=new int[array.length+a];
        for(int i=0;i<array.length;i++){
            newarray[i]=array[i];
        }
        array=newarray;
    }
    public void insert(int d,int index){//插入
        if(size==array.length){
            resize(5);
        }
        for(int i=size-1;i>=index;i--){
            array[i+1]=array[i];
        }
        array[index]=d;
        size++;

    }
    public int[] delete(int index){
        if(index<0||index>=size){
            throw new RuntimeException("超出范围");
        }
        int de=array[index];
        for(int i=index;i<size-1;i++){
            array[i]=array[i+1];
        }
        size--;
        return array;
    }
    public void output(){//输出
        System.out.println(Arrays.toString(this.array));
    }
    public static void main(String[] args) {
        Myarray myarray=new Myarray(5);
        myarray.add(1);
        myarray.add(2);
        myarray.add(3);
        myarray.add(4);
        myarray.insert(5,4);
        myarray.delete(3);
        myarray.output();


    }
}
